package Ajedrez.visual.Tablero;

import java.awt.*;
import java.awt.geom.Rectangle2D;

import javax.swing.*;

public class RelojDigital extends JPanel implements Runnable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	int size = 1;
	digitalNumber h1,h2,m1,m2,s1,s2;
	Thread th;
	boolean pulse = false;
	int hora = 0;
	int minuto = 0;
	int segundo = 0;
	JPanel panel = new JPanel();
	
	public RelojDigital(String s)
	{
		h1 = new digitalNumber(20,30,size);
		h2 = new digitalNumber(60,30,size);
		m1 = new digitalNumber(110,30,size);
		m2 = new digitalNumber(150,30,size);
		s1 = new digitalNumber(200,30,size);
		s2 = new digitalNumber(240,30,size);
		
		h1.setNumber(0);
		h2.setNumber(0);
		m1.setNumber(0);
		m2.setNumber(0);
		s1.setNumber(0);
		s2.setNumber(0);
		
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		
		JLabel label = new JLabel(s);
		label.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 20));
		panel.add(label);
		this.add(panel, "South");
		
		this.setVisible(true);
		this.setSize(300, 125);
	}
	
	public void start()
	{
		if(th == null)
		{
			th = new Thread(this);
			th.start();
		}
	}
	
	public void pause()
	{
		if(th != null)
			th = null;
	}
	
	public void reiniciar()
	{
		h1.setNumber(0);
		h2.setNumber(0);
		m1.setNumber(0);
		m2.setNumber(0);
		s1.setNumber(0);
		s2.setNumber(0);
		start();
	}
	
	public void showDots(Graphics2D g2)
	{
		if(pulse)
			g2.setColor(Color.RED);
		else
			g2.setColor(new Color(230,230,230));
		g2.fill(new Rectangle2D.Double(85,25,3,3));
		g2.fill(new Rectangle2D.Double(85,35,3,3));

		g2.fill(new Rectangle2D.Double(175,25,3,3));
		g2.fill(new Rectangle2D.Double(175,35,3,3));
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2 = (Graphics2D)g;
		h1.drawNumber(g2);
		h2.drawNumber(g2);
		m1.drawNumber(g2);
		m2.drawNumber(g2);
		s1.drawNumber(g2);
		s2.drawNumber(g2);
		showDots(g2);
	}

	@Override
	public void run()
	{
		while(th != null)
		{
			try
			{
				seeTime();
				if(pulse)
					pulse = false;
				else
					pulse = true;
				repaint();
				Thread.sleep(1000);
			}
			catch(Exception e)
			{}
		}
	}
	
	public void seeTime()
	{
		segundo ++;
		if(segundo == 60)
		{
			segundo = 0;
			minuto ++;
		}
		if(segundo < 10)
		{
			s1.setNumber(0);
			s2.setNumber(segundo);
		}
		else
			if(segundo < 20 && segundo >= 10)
			{
				s1.setNumber(1);
				s2.setNumber(segundo - 10);
			}
			else
				if(segundo < 30 && segundo >= 20)
				{
					s1.setNumber(2);
					s2.setNumber(segundo - 20);
				}
				else
					if(segundo < 40 && segundo >= 30)
					{
						s1.setNumber(3);
						s2.setNumber(segundo - 30);
					}
					else
						if(segundo < 50 && segundo >= 40)
						{
							s1.setNumber(4);
							s2.setNumber(segundo - 40);
						}
						else
							if(segundo < 60 && segundo >= 50)
							{
								s1.setNumber(5);
								s2.setNumber(segundo - 50);
							}
		if(minuto == 60)
		{
			minuto = 0;
			hora ++;
		}
		if(minuto < 10)
		{
			m1.setNumber(0);
			m2.setNumber(minuto);
		}
		else
			if(minuto < 20 && minuto >= 10)
			{
				m1.setNumber(1);
				m2.setNumber(minuto - 10);
			}
			else
				if(minuto < 30 && minuto >= 20)
				{
					m1.setNumber(2);
					m2.setNumber(minuto - 20);
				}
				else
					if(minuto < 40 && minuto >= 30)
					{
						m1.setNumber(3);
						m2.setNumber(minuto - 30);
					}
					else
						if(minuto < 50 && minuto >= 40)
						{
							m1.setNumber(4);
							m2.setNumber(minuto - 40);
						}
						else
							if(minuto < 60 && minuto >= 50)
							{
								m1.setNumber(5);
								m2.setNumber(minuto - 50);
							}
		if(hora < 10)
		{
			h1.setNumber(0);
			h2.setNumber(hora);
		}
		else
			if(hora >= 10 && hora < 20)
			{
				h1.setNumber(1);
				h2.setNumber(hora - 10);
			}
			else
				if(hora < 24 && hora >= 20)
				{
					h1.setNumber(2);
					h2.setNumber(hora - 20);
				}
	}

	public static void main(String[] args)
	{
		JFrame f = new JFrame();
		RelojDigital r = new RelojDigital("Blancas");
		r.start();
		f.add(r);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(300, 100);
		f.setLocationRelativeTo(null);
	}

}
class digitalNumber
{
	int x,y;
	int k;
	led[] leds;
	
	public digitalNumber(int x, int y, int k){
		this.x=x;
		this.y=y;
		this.k=k;
		leds = new led[7];
		leds[0] = new led(x,y,"vert");
		leds[1] = new led(x,y+10*k,"vert");
		leds[2] = new led(x+8*k,y,"vert");
		leds[3] = new led(x+8*k,y+10*k,"vert");
		leds[4] = new led(x+2*k,y-9*k,"horiz");
		leds[5] = new led(x+2*k,y+k,"horiz");
		leds[6] = new led(x+2*k,y+11*k,"horiz");
	}
	
	public void setNumber(int num)
	{
		if(num==0)
		{
			leds[0].setState(true);
			leds[1].setState(true);
			leds[2].setState(true);
			leds[3].setState(true);
			leds[4].setState(true);
			leds[5].setState(false);
			leds[6].setState(true);
		}
		else
			if(num==1)
			{
				leds[0].setState(false);
				leds[1].setState(false);
				leds[2].setState(true);
				leds[3].setState(true);
				leds[4].setState(false);
				leds[5].setState(false);
				leds[6].setState(false);			
			}
			else
				if(num==2)
				{
					leds[0].setState(false);
					leds[1].setState(true);
					leds[2].setState(true);
					leds[3].setState(false);
					leds[4].setState(true);
					leds[5].setState(true);
					leds[6].setState(true);			
				}
				else
					if(num==3)
					{
						leds[0].setState(false);
						leds[1].setState(false);
						leds[2].setState(true);
						leds[3].setState(true);
						leds[4].setState(true);
						leds[5].setState(true);
						leds[6].setState(true);			
					}
					else
						if(num==4)
						{
							leds[0].setState(true);
							leds[1].setState(false);
							leds[2].setState(true);
							leds[3].setState(true);
							leds[4].setState(false);
							leds[5].setState(true);
							leds[6].setState(false);			
						}
						else
							if(num==5)
							{
								leds[0].setState(true);
								leds[1].setState(false);			
								leds[2].setState(false);
								leds[3].setState(true);
								leds[4].setState(true);
								leds[5].setState(true);
								leds[6].setState(true);			
							}
							else
								if(num==6)
								{
									leds[0].setState(true);
									leds[1].setState(true);			
									leds[2].setState(false);
									leds[3].setState(true);
									leds[4].setState(true);
									leds[5].setState(true);
									leds[6].setState(true);			
								}
								else
									if(num==7)
									{
										leds[0].setState(false);
										leds[1].setState(false);
										leds[2].setState(true);
										leds[3].setState(true);
										leds[4].setState(true);
										leds[5].setState(false);
										leds[6].setState(false);		
									}
									else
										if(num==8 )
										{
											leds[0].setState(true);
											leds[1].setState(true);
											leds[2].setState(true);
											leds[3].setState(true);
											leds[4].setState(true);
											leds[5].setState(true);
											leds[6].setState(true);		
										}
										else
											if(num==9)
											{
												leds[0].setState(true);
												leds[1].setState(false);
												leds[2].setState(true);
												leds[3].setState(true);
												leds[4].setState(true);
												leds[5].setState(true);
												leds[6].setState(true);		
											}
	}
	
	public void turnOffNumber()
	{
		for(int i=0;i<7;i++){
			leds[i].setState(false);
		}
	}
	
	public void drawNumber(Graphics2D g2)
	{
		for(int i=0; i<7; i++)
		{
			leds[i].render(g2);
		}
	}
	
	class led
	{
		int x, y;
		Polygon p;
		String type;
		boolean lightOn=false;
		
		public led(int x, int y, String type)
		{
			this.x=x;
			this.y=y;
			this.type=type;

			p = new Polygon();
						
			if(type=="vert")
			{
				p.addPoint(x,y);
				p.addPoint(x+k,y+k);
				p.addPoint(x+2*k,y);
				p.addPoint(x+2*k,y-8*k);
				p.addPoint(x+k,y-9*k);
				p.addPoint(x,y-8*k);
			}
			
			if(type=="horiz")
			{
				p.addPoint(x,y);
				p.addPoint(x+k,y+k);
				p.addPoint(x+5*k,y+k);
				p.addPoint(x+6*k,y);
				p.addPoint(x+5*k,y-k);
				p.addPoint(x+k,y-k);				
			}
		}
		
		public void render(Graphics2D g2)
		{
			g2.setColor(new Color(230,230,230));
			if(lightOn)g2.setColor(Color.RED);				
			g2.fillPolygon(p);
		}
		
		public void setState(boolean s)
		{
			lightOn=s;
		}
	}
}
